import { request } from "../../request/index.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    leftMune: [],
    rightMune: [],
    currentIndex: 0,
    scrollTop: 0
  },
  cate: [],
  onLoad: function (options) {
    if (!wx.getStorageSync("cates")) {
      this.getcate()
    } else {
      if (Date.now() - wx.getStorageSync("cates").time > 10000) {
        this.getcate()
      } else {
        this.cate = wx.getStorageSync("cates").data
        this.setData({
          leftMune: this.cate.map(v => v.cat_name),
          rightMune: this.cate[0].children
        })
      }
    }
  },
  async getcate() {
    // request({
    //   url: 'categories'
    // }).then(res => {
    //   wx.setStorageSync("cates", {
    //     time: Date.now(), data: res.data.message
    //   })
    //   this.cate = res.data.message
    //   this.setData({
    //     leftMune: this.cate.map(v => v.cat_name),
    //     rightMune: this.cate[0].children
    //   })
    // })
    const res = await request({ url: 'categories' })
    wx.setStorageSync("cates", {
      time: Date.now(), data: res.data.message
    })
    this.cate = res.data.message
    this.setData({
      leftMune: this.cate.map(v => v.cat_name),
      rightMune: this.cate[0].children
    })
  },
  handleitemTap(e) {
    const index = e.currentTarget.dataset.index
    this.setData({
      currentIndex: index,
      rightMune: this.cate[index].children,
      scrollTop: 0
    })
  }
})